<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 13:23
 */

namespace app\company\controller;


class Customer extends Common
{
    /*
     * 客户资源统计-列表
     */
    public function csdList(){
    	$param = input('post.');

    	$data = [];

    	if(!empty($param['mobile'])) {
    		$data['mobile'] = ['like',"%{$param['mobile']}%"];
    	}
    	$data['mobile'] = $param['mobile'];

    	if(!empty($param['user_name'])) {
    		$data['user_name'] = ['like',"%{$param['user_name']}%"];
    	}
    	$data['user_name'] = $param['user_name'];

    	$data['license_type'] = $param['license_type'];
        //页码
    	$pageNum = 1;
    	if (is_numeric(input('post.pageNum'))) {
    		$pageNum = input('post.pageNum');
    	}
    	$data['pageNum'] = $pageNum;
        //每页数量
    	$numPerPage = 20;
    	$data['numPerPage'] = $numPerPage;

    	$order_model = new \app\company\model\CsdList();
        //列表数据
    	$list = $order_model->getorderist($pageNum, $numPerPage, $param);
    	$data['list'] = $list;
        //数据总数
    	$count = $order_model->getorderount($param);
    	$data['license_number'] = $param['license_number'];
    	$data['count'] = $count;
        $data['vehicle'] = db('vehicle')->select();
    	return view('csdlist', $data);
    }

    /*
     * 客户资源统计-查看
     */
    public function custOne()
    {
    	$data               = [];
        $map['id']          = input('get.id');
        // $map['is_delete']   = 0;
        $dl = db("driving_license")
                ->field("open_id")
                ->where($map)
                ->find();
        $user = db("users")
                ->field("user_id,open_id,create_time,update_time,company_id,birthday,address,mobile")
                ->where('open_id',$dl['open_id'])
                // ->where('is_delete',0)
                ->find();
        $userwx = db("user_weixin")
                    ->where("user_id",$user['user_id'])
                    ->find();
        $user['appid'] = $userwx['appid'];
        $user['nick_name'] = $userwx['nick_name'];
        $user['gender'] = $userwx['gender'];
        $user['province'] = $userwx['province'];
        $user['city'] = $userwx['city'];
        $user['country'] = $userwx['country'];

        unset($user['company_id']);
        unset($user['user_id']);
        $data['users'] = $user;
        return view('custone', $data);
    }

    /**
	 *  列表页
	 */
	public function orderList() {

		$data = input('');
                $vehicle_identification = $data['vehicle_identification'];
                $order_ids = array();
                $driving_infos = db('driving_license')
                        ->where('vehicle_identification',$vehicle_identification)
                        ->field('order_id')
                        ->select();
                foreach ($driving_infos as $key => $value) {
                    array_push($order_ids, $value['order_id']);
                }
                $map['o.order_id'] = array('in',array_unique($order_ids));
		if ($data['is_distribut'] != '') {
			$map['o.is_distribut'] = $data['is_distribut'];
		}
                if ($data['order_sn'] != '') {
			$map['o.order_sn'] = $data['order_sn'];
		}

		$data['order_status'] = -1;
		if (is_numeric(input('post.order_status'))) {
			$data['order_status'] = input('post.order_status');
		}


		if($data['order_status'] != -1 ) {
			$map['o.order_status'] = $data['order_status'];
		}
//                else{
//                        $map['o.order_status'] = ['in','2,3'];
//                }

		$map['o.order_sn'] = ['like',"%{$data['order_sn']}%"];

		if(!empty($data['start_time']) && !empty($data['end_time'])) {
			$map['o.create_time'] = ['between time',[$data['start_time'],$data['end_time']]];
		} elseif (!empty($data['start_time']) && empty($data['end_time'])) {
			$map['o.create_time'] = ['between time',[$data['start_time'],date("Y-m-d H:i:s")]];
		} elseif (empty($data['start_time']) && !empty($data['end_time'])) {
			$map['o.create_time'] = ['between time',["1970-01-01 00:00:00",$data['end_time']]];
		}

		//页码
		$pageNum = 1;
		if (is_numeric(input('post.pageNum'))) {
			$pageNum = input('post.pageNum');
		}
        //每页数量
		$numPerPage = 20;
		$data['numPerPage'] = $numPerPage;

		$map['o.company_id'] = session('company.company_id');
		$list = db('order')
                    ->alias('o')
                    ->join('driving_license d', 'd.order_id = o.order_id')
                    ->where($map)
                    ->order('d.update_time desc')
                    ->page($pageNum, $numPerPage)
                    ->select();
		$count = db('order')
                    ->alias('o')
                    ->join('driving_license d', 'd.order_id = o.order_id','LEFT')
                    ->where($map)
                    ->count();

		$data['list'] = $list;
		$data['count'] = $count;

		return view('orderlist',$data);
	}
}